<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>
<body>
  <script>
    //原型链继承 
    // 问题.引用类型的属性被所有实例共享
    function Animal (name){
      this.name = name || '动物'
      this.colors = ['red', 'blue', 'green'];
    }
    Animal.prototype.getName = function(){
      console.log(this.name)
    }
    function Cat() {

    }
    Cat.prototype = new Animal()
    var cat1 = new Cat()
    cat1.name= '猫'

    // cat1.getName()
    
    //构造函数继承 解决了原型链继承的问题 
    // 产生新的缺点方法都在构造函数中定义，每次创建实例都会创建一遍方法。
    function Dog(name){
      Animal.call(this,name)
    }
    var dog1 = new Animal()
    var dog2 = new Animal()   
    dog1.colors.push('green')
    console.log(dog1.colors+'+++'+dog2.colors)


    //组合继承 原型链和经典继承双剑合璧
    function Desk(name){
      Animal.call(this,name)
    }
    Desk.prototype = new Animal()

    //原型是继承  类比于createobj
    function createObj(o) {
      function F(){}
      F.prototype = 0
      return new F()
    }
    //即寄生继承
    function createObj (o) {
    var clone = Object.create(o);
    clone.sayName = function () {
        console.log('hi');
    }
    return clone;
}
 // 寄生组合继承
 function Parent (name) {
    this.name = name;
    this.colors = ['red', 'blue', 'green'];
}

Parent.prototype.getName = function () {
    console.log(this.name)
}

function Child (name, age) {
    Parent.call(this, name);
    this.age = age;
}

// 关键的三步
var F = function () {};

F.prototype = Parent.prototype;

Child.prototype = new F();


var child1 = new Child('kevin', '18');

console.log(child1);
  </script>
</body>
</html>